/**
 * 
 */
package com.itinsys.enma.manager.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.itinsys.enma.dao.SubjectDAO;
import com.itinsys.enma.domain.Subject;
import com.itinsys.enma.domain.response.EnmaSubjectResponse;
import com.itinsys.enma.manager.ResponseManager;
import com.itinsys.enma.manager.SubjectManager;

/**
 * @author rperez
 *
 */
public class SubjectManagerImpl implements SubjectManager{

	@Autowired
	private SubjectDAO schoolDAO;
	@Autowired
	private ResponseManager responseManager;
	
	@Override
	public EnmaSubjectResponse getAllSubjects() {
		EnmaSubjectResponse response = new EnmaSubjectResponse();
		List<Subject> subject = schoolDAO.getAll();
		if(subject.isEmpty())
			responseManager.prepareResponse(response, EnmaSubjectResponse.SUBJECT_NOT_FOUND);
		else{
			responseManager.prepareResponse(response, EnmaSubjectResponse.OK);
			response.setSubjects(subject);
		}
		return response;
	}


	@Override
	public EnmaSubjectResponse addSubject(Subject school) {
		
		EnmaSubjectResponse response = new EnmaSubjectResponse();
			if(schoolDAO.addSubject(school))		
				response=getAllSubjects();
			else
				responseManager.prepareResponse(response, EnmaSubjectResponse.ERROR);
		return response;
	}

	@Override
	public EnmaSubjectResponse updateSubject(Subject school) {
		EnmaSubjectResponse response = new EnmaSubjectResponse();
		Subject schoolToUpdate = schoolDAO.getById(String.valueOf(school.getId()));
		schoolToUpdate.setDescription(school.getDescription());
		schoolToUpdate.setCareerYear(school.getCareerYear());
		
		if(schoolDAO.updateSubject(schoolToUpdate))		
			response=getAllSubjects();
		else
			responseManager.prepareResponse(response, EnmaSubjectResponse.ERROR);

		return response;
	}

	
	@Override
	public EnmaSubjectResponse deleteSubject(Subject school) {
		EnmaSubjectResponse response = new EnmaSubjectResponse();
		
		school = schoolDAO.getById(String.valueOf(school.getId()));
		if(schoolDAO.deleteSubject(school))		
			response = getAllSubjects();
		else
			responseManager.prepareResponse(response, EnmaSubjectResponse.ERROR);
		
		return response;
	}

}
